<?php
abstract class Message{
	protected $msgId;
	protected $senderPeId;
	protected $receiverPeId;
	protected $message;
	protected $messageTitle;
	protected $senderName;
	protected $senderFirstName;
	//protected $db = new mysqli('localhost', 'root', 'gibbiX12345', 'msghub');
	protected $db;
	//protected $result;

	function __construct($messageId){
		$this->msgId		= $messageId;
		$this->db		= new mysqli('localhost', 'root', 'gibbiX12345', 'msghub');

		$this->fetchMessageDate();
		$this->fetchSenderData();
	}

	/*
	 * I would like to add e second constructor, but PHP does not support that.
	 * There are workarounds for that but I don't have the time to do it now.
	function __construct($message, $messageTitle, $senderPeId, $receiverPeId){
		$this->db = new mysqli('localhost', 'root', 'gibbiX12345', 'msghub');

		$this->message = $message;
		$this->messageTitle = $messageTitle;
		$this->senderPeId = $senderPeId;
		$this->receiverPeId = $receiverPeId;
	}*/

	function fetchMessageDate(){
		$query			= "SELECT * FROM `Messages` WHERE MsgId = ".$this->msgId;
		$result			= $this->db->query($query);
		$row			= $result->fetch_assoc();

		$this->message		= $row['Message'];
		$this->messageTitle	= $row['Title'];
		$this->receiverPeId	= $row['ReceiverPeId'];
		$this->senderPeId	= $row['SenderPeId'];
	}

	/* Don't execute this before setting the $receiverPeId :D
	 * You'll probably want to override this function in child-classes
	 * Atm this function is not actually needed in this class...
	function fetchReceiverData(){
		$query			= "SELECT * FROM `Person` WHERE PeId = ".$this->receiverPeId;
		$result			= $this->db->query($query);
		$row			= $result->fetch_assoc();
	}*/

	function fetchSenderData(){
		$query			= "SELECT * FROM `Person` WHERE PeId = ".$this->senderPeId;
		$result			= $this->db->query($query);
		$row			= $result->fetch_assoc();

		$this->senderName	= $row['Name'];
		$this->senderFirstName	= $row['Firstname'];
	}

	public function getMsgId(){
		return $this->msgId;
	}
	public function getSenderPeId(){
		return $this->senderPeId;
	}
	public function getReceiverPeId(){
		return $this->receiverPeId;
	}
	public function getMessage(){
		return $this->message;
	}
	public function getMessageTitle(){
		return $this->messageTitle;
	}
	public function getSenderName(){
		return $this->senderName;
	}
	public function getSenderFirstName(){
		return $this->senderFirstName;
	}
}
?>
